#ifdef WIN32
#include <DIRECT.H>
#endif

#ifdef LINUX
#include <unistd.h>
#endif

#include "constant.h"
#include "config.h"
#include "log.h"
#include "database.h"
#include "lock.h"

void config();
int init_server();
int init_ser_db();
struct request *accept_request();
void handle_request();
void destory_ser_db();

int main(int argc, char **argv)
{
	struct request *req;

	config();
	init_db();
	if (g_config.db_init) 
		return BS_ERROR;

	chdir(g_config.bs_home);

	g_config.sfd = init_server( );
	if ( BS_ERROR == g_config.sfd )
		return BS_ERROR;
	LOG_DEBUG("init server socket success\n");
	
	for (;;) {
		
		req = accept_request(g_config.sfd);
		
		if (!req)
			continue;
		
		handle_request(req);
	}
	
	destroy_db();
	
	return BS_OK;
}
